//
//  LableWithRedStarView.swift
//  ezyform
//
//  Created by Moennig Q on 6/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit 
class LabelWithRedStarView: UIView {
    
    let isRequiredImageViewSize: CGFloat = 7
    let isRequiredImageView: UIImageView = UIImageView(image: UIImage(named: "checkListCellStar"))
    let titleLabel = ViewFactory.shared.generateLabel("", textColor: UIColor(hexStr: Constant.RequiredTitleColorHexStr))
    var titleLabelLeftConstraint: Constraint?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpView() {
        //label
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            self.titleLabelLeftConstraint = make.left.equalTo(isRequiredImageViewSize + 3).constraint
            make.top.bottom.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        //isRequiredImageView
        self.addSubview(isRequiredImageView)
        isRequiredImageView.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.width.height.equalTo(isRequiredImageViewSize)
            make.centerY.equalTo(titleLabel).offset(-2)
        }
    }
    init(_ title: String) {
        super.init(frame: .zero)
        self.setUpView()
        self.titleLabel.text = title
        self.titleLabel.numberOfLines = 0
    }
    
    func showRedStar(_ showRedStarFlag: Bool = true){
        if showRedStarFlag {
            isRequiredImageView.isHidden = false
            titleLabelLeftConstraint?.update(offset: isRequiredImageViewSize + 3)
        }else{
            isRequiredImageView.isHidden = true
            titleLabelLeftConstraint?.update(offset: 0)
            
        }
    }
}
